<?php

/*****************************************
 * 				STATUS MASKS
 *****************************************/

define('BLG_MESSAGE_MASK_LENGTH', 5);

/**
 * Returns mask INT value
 * 
 * @param [0|1] $deleted_sender
 * @param [0|1] $deleted_recipient
 * @param [0|1] $read_sender
 * @param [0|1] $read_recipient
 * @return Int
 * @author Pablo
 * @since 1.0
 */
function blg_messages_mask($deleted_sender=0, $deleted_recipient=0, $read_sender=1, $read_recipient=0){	
	$mask= array(
		'positive'			=> 0,
		'deleted_sender' 	=> ($deleted_sender?1:0),
		'deleted_recipient'	=> ($deleted_recipient?1:0),
		'read_sender'		=> ($read_sender?1:0),
		'read_recipient'	=> ($read_recipient?1:0)
	);

	$status= implode('', $mask);
	
	return $status;	
}

/**
 * Returns mask VALUES
 * @param Int $status
 * @return Array
 * @author Pablo
 * @since 1.0
 */
function blg_messages_unmask($status){	
	list($positive, $deleted_sender, $deleted_recipient, $read_sender, $read_recipient)= str_split($status);
	
	$mask= array(
		'positive'			=> $positive,
		'deleted_sender' 	=> $deleted_sender,
		'deleted_recipient'	=> $deleted_recipient,
		'read_sender'		=> $read_sender,
		'read_recipient'	=> $read_recipient
	);	
	
	return $mask;
}

/**
 * Mask operations
 * @param $operation
 * @param $status
 * @return $status
 * @author Pablo
 * @since 1.0
 */
function blg_messages_mask_ops($operation, $status){
	$status= bindec($status);
	
	switch ($operation){
		case 'delete_sender':
			$status = ( $status | bindec('01000') ); break;
		case 'undelete_sender':
			$status = ( $status & bindec('10111') ); break;		
		case 'delete_recipient':
			$status = ( $status | bindec('00100') ); break;
		case 'undelete_recipient':
			$status = ( $status & bindec('11011') ); break;		
		case 'read_sender':
			$status = ( $status | bindec('00010') ); break;
		case 'unread_sender':
			$status = ( $status & bindec('11101') ); break;		
		case 'read_recipient':
			$status = ( $status | bindec('00001') ); break;
		case 'unread_recipient':
			$status = ( $status & bindec('11110') ); break;
	}
	
	$status= decbin($status);
	
	for($i=(BLG_MESSAGE_MASK_LENGTH-strlen($status)); $i--; $i>0 )
		$status= '0'.$status;	

	return $status;
}

/*****************************************
 * 			  MESSAGES FUNCTION
 *****************************************/

/**
 * Create/Send a new message
 * 
 * @param int $from_guid
 * @param int $to_guid
 * @param string $subject
 * @param string $message
 * @param [int|null] $reply_id
 * @return FALSE|int Returns message ID on success, FALSE otherwise
 * @author Pablo
 * @since 1.0 
 */
function blg_message_new($from_guid, $to_guid, $subject, $message, $reply_id= null, $status= null){
	if(FALSE!==($subject_id= blg_metastring_add($subject))){
		$values= array(
			'from_guid'	=> $from_guid,
			'to_guid' 	=> $to_guid,
			'subject' 	=> $subject_id,
			'message' 	=> $message,
			'status'	=> (empty($status)?blg_messages_mask(0,0,1,0):$status)
		);
		
		if(is_int($reply_id))
			$values['reply_id']= $reply_id; 
		
		$query= blg_dbschema_query_buid(blg_dbschema_insert_message($values));
		$result= blg_dbschema_insert($query);
		
		if($result->success)
			return (int) $result->new_id;
	}
	
	return FALSE;
}

/**
 * Delete a message
 * @param $id
 * @return bool
 * @author Pablo
 * @since 1.0 
 */
function blg_message_delete($id){
	$query= blg_dbschema_query_buid(blg_dbschema_delete_messages(array(
		'id' => $id
	)));
	$result= blg_dbschema_delete($query);
	return ($result->success);
}

/**
 * Delete a set of messages
 * @param array $messages (Array of <i>BlgMessage</i> elements)
 * @return bool
 * @author Pablo
 * @since 1.0
 */
function blg_messages_delete($messages){
	$result= TRUE;
	foreach($messages as $message){
		$result= $result && blg_message_delete($message->id);
	}
	return $result;
}

/**
 * Select messages from/to a user
 * @param int $user_guid
 * @param bool $recipientSender (TRUE)
 * @param [null|bool] $readUnread (NULL)
 * @return [FALSE|Array]
 * @author Pablo
 * @since 1.0 
 */
function blg_messages_user($user_guid, $recipientSender=true, $readUnread=NULL){
	//BUILD MASK
	$mask= '0____';
	
	if(NULL!==$readUnread){
		if($recipientSender){ //RECIPIENT		
			$mask{4}= ($readUnread?'1':'0');
		}
		else{ //SENDER
			$mask{3}= ($readUnread?'1':'0');			
		} 
	}
	
	// BUILD QUERY
	$query= blg_dbschema_query_buid(blg_dbschema_select_messages(
		array(
			($recipientSender?'to_guid':'from_guid') => $user_guid,
			'status' => $mask
		),
		array('id')
	));
	$result= blg_dbschema_select($query);
	
	//BUILD RESULTS
	if($result->success){
		$messages= array();
		for($i=0; $i<$result->num_rows; $i++){
			$id= $result->{$i}['id'];
			$messages[]= new BlgMessage($id);
		}
		return $messages;
	}
	
	return FALSE;
}

/**
 * Check if message exists by id
 * @param int $id
 * @return bool
 * @author Pablo
 * @since 1.0 
 */
function blg_message_exists($id){
	$query= blg_dbschema_query_buid(blg_dbschema_select_messages(array('id' => $id)));
	$result= blg_dbschema_select($query);	
	return ($result->success && $result->num_rows==1);		
}



